package com.fallback;

import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.SortedSet;

/**
 * @author wbzs,
 * @date 2022/10/23,
 * @time 18:06,
 */
@Component
public class MyfallBackProvider implements FallbackProvider {
    /*
    我们可以具体的路由服务名，针对的是这个具体的服务，也可以返回*,代表所有的服务
     */
    @Override
    public String getRoute() {
        return "*";
    }
/*
这个就是发生问题的时候返回客户端的数据
 */
    @Override
    public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
        return new ClientHttpResponse() {
            @Override
            public HttpStatus getStatusCode() throws IOException {
                return HttpStatus.OK;
            }

            @Override
            public int getRawStatusCode() throws IOException {
                return 200;
            }

            @Override
            public String getStatusText() throws IOException {
                return "no ok";
            }

            @Override
            public void close() {

            }

            //响应正文
            @Override
            public InputStream getBody() throws IOException {
                return new FileInputStream("这是响应体");
            }

            //相应头
            @Override
            public HttpHeaders getHeaders() {
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.add("zheshixiangyingtou","zheshitoushuju");
                httpHeaders.add("content-type","text/html;charset=utf-8");
                return httpHeaders;
            }
        };
    }
}
